<!--
 * @Author: HeAo
 * @Date: 2021-10-11 10:04:12
 * @LastEditTime: 2021-10-11 11:13:22
 * @LastEditors: HeAo
 * @Description: 
 * @FilePath: \web2103-2105\web2105\3-2-Vue\day03\2-组件通信.html
 * 别乱动！
-->
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="../js/vue.js"></script>
</head>

<body>
  <div id="app">
    <!-- 父组件通过属性绑定的形式将目标数据传递给子组件 -->
    <!-- <my-button :attr1="attr1"></my-button> -->
    <my-button :attr1="attr1" :attr2="1"></my-button>
    <div>
      <button @click="attr1 = '单向数据流'">修改根组件数据</button>
    </div>
  </div>
  <script>
    // 1.组件定义
    let component1 = {
      // 子组件通过props属性，来接收父组件传递下来的数据
      // props: ['attr1'],
      props: {
        // attr1: [String, Number]
        attr1: {
          type: [String],
          // required: true,
          default: 'hello default',
          validator(value) {
            return value.length > 3
          }
        },
        attr2: {
          default: ''
        },
        obj: {
          type: Object,
          default() {
            return {
              username: 'Lisi',
              age: 20
            }
          }
        }
      },
      data() {
        return {
          msg: '组件1数据'
        }
      },
      template: `<div>
          <button>{{msg}}</button>
          <button>{{attr1}}</button>
          <div>用户信息：{{obj}}</div>
          {{attr2}}-{{typeof attr2}}
        </div>`
    }

    let vm = new Vue({
      el: '#app',
      data: {
        attr1: '1234',
        obj: {
          username: 'zhangsan',
          age: 12
        }
      },
      methods: {

      },
      created() {

      },
      // 2.组件局部注册
      components: {
        'my-button': component1
      }
    })
  </script>
</body>

</html>